package com.ming.closeto.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.ming.closeto.R;

/**
 * 主页
 */
public class HomeFragment extends Fragment implements BottomNavigationView.OnNavigationItemSelectedListener {
    private BottomNavigationView mBottomNavigationView;

    private SelectFragment selectFragment;
    private MusicFragment musicFragment;
    private MovieFragment movieFragment;
    private MeFragment meFragment;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mBottomNavigationView = (BottomNavigationView) view.findViewById(R.id.mBottomNavigationView);

        // 设置监听
        mBottomNavigationView.setOnNavigationItemSelectedListener(this);
        // 去掉主题色渲染
        //mBottomNavigationView.setItemIconTintList(null);
        // 去掉动画
//        BottomNavigationViewHelper.disableShiftMode(mBottomNavigationView);

        // 默认显示第一页 SelectFragment
        showSelectFragment();
    }

    /**
     * 底部导航栏监听
     * @param menuItem
     * @return
     */
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.menu_select:
                showSelectFragment();
                break;
            case R.id.menu_music:
                showMusicFragment();
                break;
            case R.id.menu_movie:
                showMovieFragment();
                break;
            case R.id.menu_me:
                showMeFragment();
                break;
        }
        return true;
    }

    private void showSelectFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (selectFragment == null) {
            selectFragment = new SelectFragment();
            ft.add(R.id.home_content_layout, selectFragment);
        }
        hideAllFragment(ft);
        ft.show(selectFragment);
        ft.commit();
    }
    private void showMusicFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (musicFragment == null) {
            musicFragment = new MusicFragment();
            ft.add(R.id.home_content_layout, musicFragment);
        }
        hideAllFragment(ft);
        ft.show(musicFragment);
        ft.commit();
    }
    private void showMovieFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (movieFragment == null) {
            movieFragment = new MovieFragment();
            ft.add(R.id.home_content_layout, movieFragment);
        }
        hideAllFragment(ft);
        ft.show(movieFragment);
        ft.commit();
    }
    private void showMeFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (meFragment == null) {
            meFragment = new MeFragment();
            ft.add(R.id.home_content_layout, meFragment);
        }
        hideAllFragment(ft);
        ft.show(meFragment);
        ft.commit();
    }

    /**
     * 隐藏所有Fragment
     * @param fb
     */
    private void hideAllFragment(FragmentTransaction fb) {
        if (selectFragment != null) {
            fb.hide(selectFragment);
        }
        if (musicFragment != null) {
            fb.hide(musicFragment);
        }
        if (movieFragment != null) {
            fb.hide(movieFragment);
        }
        if (meFragment != null) {
            fb.hide(meFragment);
        }
    }

    // 去除BottomNavigation动画
    public static class BottomNavigationViewHelper {

        @SuppressLint("RestrictedApi")
        public static void disableShiftMode(BottomNavigationView view) {

            BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
            try {
                menuView.setLabelVisibilityMode(1);
                for (int i = 0; i < menuView.getChildCount(); i++) {
                    BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                    item.setShifting(false);
                }
            } catch (Exception e) {

            }
        }
    }


}
